function HeatPoint(x,y){
    this.X = x;
    this.Y = y;
    if(HeatPoint.firstClickTime < 0 || HeatPoint.firstClickTime  === undefined){
        HeatPoint.firstClickTime = new Date().getTime();
    }
    this.TimeStamp = (new Date().getTime())- HeatPoint.firstClickTime;

    this.getTime = function(){
        return this.TimeStamp;
    };
    this.asArray = function() {
        return new Array(this.X,this.Y,this.TimeStamp);
    };
}
var KEY = {
    'BACKSPACE': 8,
    'TAB': 9,
    'ENTER': 13,
    'SHIFT': 16,
    'CTRL': 17,
    'ALT': 18,
    'PAUSE': 19,
    'CAPSLOCK': 20,
    'ESC': 27,
    'PAGEUP': 33,
    'PAGEDOWN': 34,
    'END': 35,
    'HOME': 36,
    'LEFTARROW': 37,
    'UPARROW': 38,
    'RIGHTARROW': 39,
    'DOWNARROW': 40,
    'INSERT': 45,
    'DELETE': 46,
    'N0': 48,
    'N1': 49,
    'N2': 50,
    'N3': 51,
    'N4': 52,
    'N5': 53,
    'N6': 54,
    'N7': 55,
    'N8': 56,
    'N9': 57,
    'A': 65,
    'B': 66,
    'C': 67,
    'D': 68,
    'E': 69,
    'F': 70,
    'G': 71,
    'H': 72,
    'I': 73,
    'J': 74,
    'K': 75,
    'L': 76,
    'M': 77,
    'N': 78,
    'O': 79,
    'P': 80,
    'Q': 81,
    'R': 82,
    'S': 83,
    'T': 84,
    'U': 85,
    'V': 86,
    'W': 87,
    'X': 88,
    'Y': 89,
    'Z': 90,
    'UE': 186,
    'AE': 222,
    'OE': 192,
    'NUMPAD0': 96,
    'NUMPAD1': 97,
    'NUMPAD2': 98,
    'NUMPAD3': 99,
    'NUMPAD4': 100,
    'NUMPAD5': 101,
    'NUMPAD6': 102,
    'NUMPAD7': 103,
    'NUMPAD8': 104,
    'NUMPAD9': 105,
    'MULTIPLY': 106,
    'PLUS': 107,
    'MINUS': 109,
    'NUMPADDOT': 110,
    'SLASH1': 111,
    'F1': 112,
    'F2': 113,
    'F3': 114,
    'F4': 115,
    'F5': 116,
    'F6': 117,
    'F7': 118,
    'F8': 119,
    'F9': 120,
    'F10': 121,
    'F11': 122,
    'F12': 123,
    'MULT_PLUS': 187,
    'COMA': 188,
    'HIGHCOMA': 221,
    'QUESTIONMARK': 219,
    'HASH': 191,
    'DOT': 190,
    'UNDERLINE': 189,
    'TILT': 220,
    'SPACE': 32,
    'NUMPAD': 144,
    'PIPE': 226
}

function KeyHitPoint(xKey,X,Y){
    this.keyCode    = xKey;
    this.X      = X;
    this.Y      = Y;
    this.Count  = 0;

    this.setCount = function(xCount){
        this.Count = xCount;
    };
    this.addStroke = function(){
        this.Count++; 
    };
}

function initKeyList(keyHitPointList){
    var linestart = 0;
    var distance = 36;
    var height = 0;

    // keyHitPointList.push(new KeyHitPoint(KEY.PAUSE,       0,0));
    // keyHitPointList.push(new KeyHitPoint(KEY.ESC,         0,0));
    // keyHitPointList.push(new KeyHitPoint(KEY.F1,          52,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F2,          87,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F3,          87,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F4,          122,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F5,          157,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F6,          192,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F7,          227,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F8,          262,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F9,          297,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F10,         332,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F11,         367,17));
    // keyHitPointList.push(new KeyHitPoint(KEY.F12,         4172,17));

    linestart = 43.25;
    distance = 35.75;
    height = 45;

    keyHitPointList.push(new KeyHitPoint(KEY.TILT,        linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N1,          linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N2,          linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N3,          linestart +(distance*3),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N4,          linestart +(distance*4),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N5,          linestart +(distance*5),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N6,          linestart +(distance*6),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N7,          linestart +(distance*7),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N8,          linestart +(distance*8),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N9,          linestart +(distance*9),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N0,          linestart +(distance*10),height));
    keyHitPointList.push(new KeyHitPoint(KEY.QUESTIONMARK,linestart +(distance*11),height));
    keyHitPointList.push(new KeyHitPoint(KEY.HIGHCOMA,    linestart +(distance*12),height));
    keyHitPointList.push(new KeyHitPoint(KEY.BACKSPACE,    linestart+(distance*13),height));


    linestart = 96;
    distance = 35.75;
    height = 87;
    keyHitPointList.push(new KeyHitPoint(KEY.TAB,         52,height));
    keyHitPointList.push(new KeyHitPoint(KEY.Q,           linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.W,           linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.E,           linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.R,           linestart +(distance*3),height));
    keyHitPointList.push(new KeyHitPoint(KEY.T,           linestart +(distance*4),height));
    keyHitPointList.push(new KeyHitPoint(KEY.Z,           linestart +(distance*5),height));
    keyHitPointList.push(new KeyHitPoint(KEY.U,           linestart +(distance*6),height));
    keyHitPointList.push(new KeyHitPoint(KEY.I,           linestart +(distance*7),height));
    keyHitPointList.push(new KeyHitPoint(KEY.O,           linestart +(distance*8),height));
    keyHitPointList.push(new KeyHitPoint(KEY.P,           linestart +(distance*9),height));
    keyHitPointList.push(new KeyHitPoint(KEY.UE,          linestart +(distance*10),height));
    keyHitPointList.push(new KeyHitPoint(KEY.MULT_PLUS,   linestart +(distance*11),height));

    linestart = 108;
    distance = 35.75;
    height = 130;
    keyHitPointList.push(new KeyHitPoint(KEY.CAPSLOCK,    58,height));
    keyHitPointList.push(new KeyHitPoint(KEY.A,           linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.S,           linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.D,           linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.F,           linestart +(distance*3),height));
    keyHitPointList.push(new KeyHitPoint(KEY.G,           linestart +(distance*4),height));
    keyHitPointList.push(new KeyHitPoint(KEY.H,           linestart +(distance*5),height));
    keyHitPointList.push(new KeyHitPoint(KEY.J,           linestart +(distance*6),height));
    keyHitPointList.push(new KeyHitPoint(KEY.K,           linestart +(distance*7),height));
    keyHitPointList.push(new KeyHitPoint(KEY.L,           linestart +(distance*8),height));
    keyHitPointList.push(new KeyHitPoint(KEY.OE,          linestart +(distance*9),height));
    keyHitPointList.push(new KeyHitPoint(KEY.AE,          linestart +(distance*10),height));
    keyHitPointList.push(new KeyHitPoint(KEY.HASH,        linestart +(distance*11),height));
    keyHitPointList.push(new KeyHitPoint(KEY.ENTER,       linestart +(distance*12),height));

    linestart = 90;
    distance = 35.75;
    height = 174;

    keyHitPointList.push(new KeyHitPoint(KEY.SHIFT,       48,height));
    keyHitPointList.push(new KeyHitPoint(KEY.PIPE,        linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.Y,           linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.X,           linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.C,           linestart +(distance*3),height));
    keyHitPointList.push(new KeyHitPoint(KEY.V,           linestart +(distance*4),height));
    keyHitPointList.push(new KeyHitPoint(KEY.B,           linestart +(distance*5),height));
    keyHitPointList.push(new KeyHitPoint(KEY.N,           linestart +(distance*6),height));
    keyHitPointList.push(new KeyHitPoint(KEY.M,           linestart +(distance*7),height));
    keyHitPointList.push(new KeyHitPoint(KEY.COMA,        linestart +(distance*8),height));
    keyHitPointList.push(new KeyHitPoint(KEY.DOT,         linestart +(distance*9),height));
    keyHitPointList.push(new KeyHitPoint(KEY.UNDERLINE,   linestart +(distance*10),height));
    

    linestart = 747;
    distance = 35.75;
    height = 48;
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD,      linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.SLASH1,      linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.MULTIPLY,    linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.MINUS,       linestart +(distance*3),height));

    height = 90;

    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD7,     linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD8,     linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD9,     linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.PLUS,        linestart +(distance*3),height));
    
    height = 133;
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD4,     linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD5,     linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD6,     linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.PLUS,        linestart +(distance*3),height));
   
    height = 176;
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD1,     linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD2,     linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD3,     linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.ENTER,       linestart +(distance*3),height));

    height = 218;
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD0,     linestart +(distance*0),height));
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPAD0,     linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.NUMPADDOT,   linestart +(distance*2),height));
    keyHitPointList.push(new KeyHitPoint(KEY.ENTER,       linestart +(distance*3),height));

    linestart = 605;
    
    height = 218;

    keyHitPointList.push(new KeyHitPoint(KEY.CTRL,        52,height));
    keyHitPointList.push(new KeyHitPoint(KEY.ALT,         146,height));
    keyHitPointList.push(new KeyHitPoint(KEY.SPACE,       294,height));
    keyHitPointList.push(new KeyHitPoint(KEY.LEFTARROW,   linestart +(distance*0),height));
    
    keyHitPointList.push(new KeyHitPoint(KEY.INSERT,      linestart +(distance*0),48));
    keyHitPointList.push(new KeyHitPoint(KEY.DELETE,      linestart +(distance*0),90));

    keyHitPointList.push(new KeyHitPoint(KEY.UPARROW,     linestart +(distance*1),height-45));
    keyHitPointList.push(new KeyHitPoint(KEY.DOWNARROW,   linestart +(distance*1),height));
    keyHitPointList.push(new KeyHitPoint(KEY.END,         linestart +(distance*1),48));
    keyHitPointList.push(new KeyHitPoint(KEY.HOME,        linestart +(distance*1),90));

    keyHitPointList.push(new KeyHitPoint(KEY.RIGHTARROW,  linestart +(distance*2),height));

    keyHitPointList.push(new KeyHitPoint(KEY.PAGEUP,      linestart +(distance*2),48));
    keyHitPointList.push(new KeyHitPoint(KEY.PAGEDOWN,    linestart +(distance*2),90));
    
    keyHitPointList.addStrokeToKey = function(xKey){
        if ( synectic.stopanalyse) return;
        for(var i = 0; i < this.length;i++)
            if(xKey == this[i].keyCode){
                this[i].addStroke();
        }
    };

    keyHitPointList.clearList = function(){
        $.each(keyHitPointList, function(index, val) {
            val.Count =0;
        });
    }
}

$(document).ready(function() {

    $('.heatmapAreaKeyPressed').each(function(index,obj){
        obj.HeatPointKeyStrokeList = new Array();
        initKeyList(obj.HeatPointKeyStrokeList);
        $(obj).keydown(function(event) {
            
            obj.HeatPointKeyStrokeList.addStrokeToKey(event.keyCode);
        });
    });

    $('.heatmapAreaCharAnalyse').each(function(index,obj){
         obj.updateCharList = function(){
            if (synectic.stopanalyse) return;
            var pText = "";
            pText = $(this).val().toUpperCase();

            for(var charCounter = 0; charCounter < this.HeatPointCharList.length;charCounter++ )
                if(this.HeatPointCharList[charCounter].keyCode != 32)
                    this.HeatPointCharList[charCounter].setCount(
                        getCharCount(
                            pText,
                            String.fromCharCode(this.HeatPointCharList[charCounter].keyCode) ));
            
            };

         /* iterate through array or object */
        obj.HeatPointCharList = new Array();
        initKeyList(obj.HeatPointCharList);
    });
});



